package demo.java.net;

import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.Socket;

public class SocketDemo {

    public static boolean telnet(String hostname, int port, int timeout) {
        try (Socket socket = new Socket()) {
            // 建立连接
            socket.connect(new InetSocketAddress(hostname, port), timeout);
            // 通过现有方法查看连通状态
            return socket.isConnected();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Test
    public void testTelnet() {
        String hostname = "192.168.60.99";
        int port = 9093;
        int timeout = 3000;
        boolean isConnected = telnet(hostname, port, timeout);
        System.out.println("isConnected = " + isConnected);
    }
}
